# -*- coding: utf-8 -*-
import random
import string
import json
import csv
import sys
total=0
level=0
def addAChildToitem(item):
	if item.has_key('children'):
		item['children'].append(createAnEndNode())
	else:
		item['children']=[]
		item['children'].append(createAnEndNode())
	return item
def createAnEndNode():
	global total
	global level
	total+=1	
	return {	
		'name':str(++total),'level':level	
	}
def createRandomInOut(node,parent):
	if parent==None:#说明这是根节点
		inVol=random.uniform(1000, 10000)
		outVol=random.uniform(1000,10000)
		while outVol>=inVol:
			outVol=random.uniform(1000,10000)
		node['in']=inVol
		node['out']=outVol
		for child in node['children']:
			createRandomInOut(child,node)
	else:		
		if node.has_key('children'):
			node['in']=(parent['out'])/len(parent['children'])
			node['out']=node['in']-0.01
			for child in node['children']:
				createRandomInOut(child,node)
		else:#端点，使用的是电表类型，电表类型没有输入，只有输出售电量
			node['out']=(parent['out'])/len(parent['children'])
			node['in']=0
			return
data={'name':'0','level':level}
level+=1
for i in xrange(0,random.randint(50,80)):
	addAChildToitem(data)	
level+=1
for j in xrange(0,len(data['children'])):
	for i in xrange(1,random.randint(15,18)):
		addAChildToitem(data['children'][j])
level+=1
for j in xrange(0,len(data['children'])):
	for i in xrange(0,len(data['children'][j]['children'])):
		for z in xrange(1,random.randint(13,16)):
			addAChildToitem(data['children'][j]['children'][i])
level+=1
for j in xrange(0,len(data['children'])):
	for i in xrange(0,len(data['children'][j]['children'])):
		for z in xrange(0,len(data['children'][j]['children'][i]['children'])):
			for k in xrange(1,random.randint(7,9)):
				addAChildToitem(data['children'][j]['children'][i]['children'][z])	
def getAllChildrenName(node):
	result=''
	if node.has_key('children'):
		for item in node['children']:
			result+=item['name']+' '
	else:
		return ''
	return result
def addRootToCsv(data,f):
	#生成标题行
	f.writerow(['name', 
		'totalnodelengthforroot','level', 
		'children','in','out'])	
	f.writerow([data['name'], 
		data['totalnodelength'], 
		data['level'],
		getAllChildrenName(data),data['in'],data['out']])	
	for item in data['children']:
		addNodeToCsv(item,f)
def addNodeToCsv(node,f):
	global total
	total+=1
	if node.has_key('in'):
		f.writerow([node['name'], 
			'', 
			node['level'],
			getAllChildrenName(node),node['in'],node['out']])
	else:
		f.writerow([node['name'], 
			'', 
			node['level'],
			getAllChildrenName(node),0,node['out']])
	if node.has_key('children'):			
		for item in node['children']:
			addNodeToCsv(item,f)
	else:
		return
if __name__ == '__main__':
	print sys.argv
	createRandomInOut(data,None)	
	data['totalnodelength']=total
	print total
	if len(sys.argv)==4:
		file=open(sys.argv[1],'w')
	else:
		file=open('data.json','w')
	file.write(json.dumps(data,sort_keys=True))
	file.close()
	if len(sys.argv)==4:
		file=open(sys.argv[2],'w')
	else:
		file=open('data2.json','w')
	file.write(json.dumps(data,sort_keys=True))
	file.close()
	if len(sys.argv)==4:
		f = open(sys.argv[2])
	else:
		f = open('data.json')
	data = json.load(f)
	f.close()
	if len(sys.argv)==4:		
		f=csv.writer(open(sys.argv[3],'wb+'))
	else:
		f=csv.writer(open('data.csv','wb+'))	
	addRootToCsv(data,f)	
